package demo02;

import java.util.*;
/**
 * 如果ArrayList调用，不带参数的构造方法，name顺序表的大小是0，当第一次add的时候，整个顺序表才变成10；
 * 当这10个放满了，开始扩容，1.5倍进行扩容。
 * 如果调用的是给定容量的构造方法，那么顺序表的大小是你传入的大小，满了就以1.5倍扩容
 */

/**
 * @program: 12.2
 * @description:
 * @author: 李鑫月
 * @create: 2021-12-04 15:14
 **/
class MyArrayList<E>{
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA ={} ;
    private Object[] elementData;
    private int usedSize;
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
    public MyArrayList(){
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }
    public MyArrayList(int capacity){
        if(capacity>=0){
            this.elementData=new Object[capacity];
        }else {
            throw new IllegalArgumentException("初始化的容量不能为负");
        }

    }

    /**
     * 添加元素,相当于存放在数组的最后位置
     * @param e
     * @return
     */
    public boolean add(E e){
        ensureCapacityInternal(usedSize+ 1);
        elementData[usedSize++]=e;
        return true;
    }
    private void ensureCapacityInternal(int minCapacity) {
        //计算出需要的容量
        int capacity=calculateCapacity(elementData, minCapacity);
       //去看满了-》扩容，空-》也是
        ensureExplicitCapacity(capacity);

    }
    private static int calculateCapacity(Object[] elementData, int minCapacity) {
        //空，先分10个
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            return Math.max(10, minCapacity);
        }
        //不空
        return minCapacity;
    }
    private void ensureExplicitCapacity(int minCapacity) {

        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);//1.5倍扩容
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }
    private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
                Integer.MAX_VALUE :
                MAX_ARRAY_SIZE;
    }

    /**
     * 给index位置设置为元素e
     * @param index
     * @param e
     */
    public void add(int index,E e){
        rangeCheckForAdd(index);
        ensureCapacityInternal(usedSize + 1);
        copy(index,e);
        usedSize++;

    }
    private void copy(int index,E e){
        for (int i = usedSize-1; i >=index; i--) {
            elementData[i+1]=elementData[i];

        }
        elementData[index]=e;
    }
    private void rangeCheckForAdd(int index) {
        if (index >usedSize || index < 0)
            throw new IndexOutOfBoundsException("index不合法");
    }



}
public class Demo04 {
    public static void main(String[] args) {
        MyArrayList<String> myArrayList=new MyArrayList<>();
        myArrayList.add("aaa");
        myArrayList.add("vaa");
        myArrayList.add("xaa");
        myArrayList.add("zaa");
        myArrayList.add("waa");
        myArrayList.add(0,"qqq");
        myArrayList.add(4,"ppp");
    }
    public static void main4(String[] args) {
        ArrayList<String>list=new ArrayList<>();//初识的大小是
        list.add("aaa");
        list.add("abba");
        list.add("accc");
        System.out.println("*****List迭代器************");
        ListIterator<String> listIterator=list.listIterator();
        while (listIterator.hasNext()){
            String ret=listIterator.next();
            if(ret.equals("aaa")){
                listIterator.add("xxx");
            }



        }
        System.out.println(list);
    }
    public static void main2(String[] args) {
        List<String>list=new ArrayList<>();
        list.add("aaa");
        list.add("abba");
        list.add("accc");
        System.out.println("*****List迭代器************");
        ListIterator<String> listIterator=list.listIterator();
        while (listIterator.hasNext()){
            String ret=listIterator.next();
            if(ret.equals("aaa")){
                listIterator.remove();
            }else {
                System.out.print(ret+" ");
            }

        }

    }
    public static void main1(String[] args) {
        List<String>list=new ArrayList<>();
        ArrayList<String>list1=new ArrayList<>();
        list1.add("aaa");
        list.add("abba");
        list.add("accc");
        System.out.println(list);
        System.out.println("*************");
        for (int i=0;i<list.size();i++){
            System.out.println(list.get(i));
        }
        System.out.println("*****************");
        for (String s:list
             ) {
            System.out.println(s+" ");
        }
        System.out.println("******迭代器***********");
        Iterator<String>iterator=list.iterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next()+" ");
        }
        System.out.println();
        System.out.println("*****List迭代器************");
        ListIterator<String> listIterator=list.listIterator();
        while (listIterator.hasNext()){
            System.out.print(listIterator.next()+" ");
        }




    }
}
